/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sunsite.action;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import sunsite.po.Message;
import sunsite.service.MessageService;
import sunsite.tools.Naming;

/**
 *
 * @author mg
 */
public class PostMessage extends ActionSupport {

    public static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Register.class);
    private String name;
    private String email;
    private String content;
    private String verifyCode;
    private String ip;
    private MessageService messageService;
    private ResourceBundleMessageSource messageSource;
    private String message = "";

    @Override
    public String execute() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
//        HttpServletResponse response = null;
//        response = ServletActionContext.getResponse();
//        request.setCharacterEncoding("utf-8");
//        response.setContentType("text/html;charset=utf-8");


        String requestCode = request.getParameter(Naming.WebParameter.VerifyCode);
        String sessionCode = (String) request.getSession().getAttribute(Naming.SessionName.VerifyCodeSession);
        if (sessionCode == null || !sessionCode.equals(requestCode)) {
            message = messageSource.getMessage("user.verifyCodeError", null, Locale.getDefault());
            return "forward";
        }

        if (name == null || email == null || content == null) {
            message = messageSource.getMessage("user.regInfoNotEnought", null, Locale.getDefault());
            return "forward";
        }
        String ip = ServletActionContext.getRequest().getRemoteAddr();
        Message m = new Message();
        m.setMesName(name);
        m.setMesId("mes" + System.nanoTime());
        m.setMesEmail(email);
        m.setMesContent(content);
        m.setMesIp(ip);
        m.setMesTime(new Date());
        messageService.addMessage(m);
        return "forward";
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }

    public void setMessageSource(ResourceBundleMessageSource messageSource) {
        this.messageSource = messageSource;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the content
     */
    public String getContent() {
        return content;
    }

    /**
     * @param content the content to set
     */
    public void setContent(String content) {
        this.content = content;
    }

    /**
     * @return the verifyCode
     */
    public String getVerifyCode() {
        return verifyCode;
    }

    /**
     * @param verifyCode the verifyCode to set
     */
    public void setVerifyCode(String verifyCode) {
        this.verifyCode = verifyCode;
    }

    /**
     * @return the ip
     */
    public String getIp() {
        return ip;
    }

    /**
     * @param ip the ip to set
     */
    public void setIp(String ip) {
        this.ip = ip;
    }
}
